﻿using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace ezCloud.Api.Common
{
    public class extededScreenClients
    {
        public  string Token { get; set; } 
        public int hotelId { get; set; }
        public dynamic client { get; set; }

    }
    [Authorize]
    public class extendedScreenHub : Hub
    {
        static List<extededScreenClients> _clients =new List<extededScreenClients>() ;
        public override Task OnConnected()
        {
            var token = Context.QueryString.Get("access_token");
            if (token != null) {
                var client = new extededScreenClients();
                client.client = Clients.Caller;
                client.Token = token;
                UserProfile up = UserUtitily.getUserProfile();
                client.hotelId = up.HotelId;
                Groups.Add(Context.ConnectionId, up.HotelId.ToString());

                return Clients.Caller.hubMessage("OnConnected " + Context.ConnectionId);
            } else
                return Clients.Caller.hubMessage("No token " + Context.ConnectionId);

        }
        public static void SendMessageToHotel(int hotelId,object obj)
        {
            var context = GlobalHost.ConnectionManager.GetHubContext<extendedScreenHub>();
            context.Clients.Group(hotelId.ToString()).sendMessage(obj);

        }
        public override Task OnReconnected()
        {
            return Clients.Caller.hubMessage("OnReconnected");
        }

        public override Task OnDisconnected(bool stopCalled)
        {
            return base.OnDisconnected(stopCalled);
        }
    }
}